﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CanEditMultipleObjects]
    [CustomEditor(typeof(NirvanaRenderer))]
    public class NirvanaRendererEditor : Editor
    {
        private ReorderableList reorderableList;
        private SerializedProperty materials;
        private SerializedProperty renderQueue;
        private SerializedProperty showBounds;
        private RenderEffect[] allRendererEffects;
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            reorderableList.DoLayoutList();
            EditorGUILayout.PropertyField(this.renderQueue, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.showBounds, new GUILayoutOption[0]);
            serializedObject.ApplyModifiedProperties();

            PrefabType prefabType = PrefabUtility.GetPrefabType(this.target);
            if (prefabType != PrefabType.Prefab && base.targets.Length == 1)
            {
                NirvanaRenderer nirvanaRenderer = (NirvanaRenderer)this.target;
                this.FindEffects(nirvanaRenderer);
            }

            if (base.targets != null && base.targets.Length > 1 && GUILayout.Button("Add Default", new GUILayoutOption[0]))
            {
                foreach (Object @object in base.targets)
                {
                    NirvanaRenderer nirvanaRenderer2 = (NirvanaRenderer)@object;
                    nirvanaRenderer2.SetDefaultMaterial();
                    EditorUtility.SetDirty(@object);
                }
            }

        }

        void OnEnable()
        {
            if (this.target == null) return;
            this.materials = serializedObject.FindProperty("materials");
            this.renderQueue = serializedObject.FindProperty("renderQueue");
            this.showBounds = serializedObject.FindProperty("showBounds");

            reorderableList = new ReorderableList(serializedObject, materials);
            reorderableList.drawElementCallback = DrawNameElement;//绘制每个元素的回调
            reorderableList.drawHeaderCallback = DrawHeader;//绘制列表表头
            reorderableList.elementHeight = 1f * EditorGUIUtility.singleLineHeight;
            reorderableList.onAddCallback = onAddCallback;

        }



        private void DrawHeader(Rect rect)
        {
            GUI.Label(rect, "Materials:");
        }

        private void DrawNameElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty arrayElementAtIndex = this.materials.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(rect, arrayElementAtIndex);
        }
        private void onAddCallback(ReorderableList _list)
        {
            NirvanaRenderer nirvanaRenderer = (NirvanaRenderer)this.target;
            nirvanaRenderer.AddDefaultMaterial();
            EditorUtility.SetDirty(this.target);
        }

        private void FindEffects(NirvanaRenderer render)
        {
            if (GUILayout.Button("Find Effects", new GUILayoutOption[0]))
            {
                this.allRendererEffects = render.FindAllEffect();
            }

            if(this.allRendererEffects != null && allRendererEffects.Length > 0)
            {
                EditorGUILayout.LabelField("Effects: ", new GUILayoutOption[0]);
                GUILayoutEx.BeginContents();

                foreach (var effect in this.allRendererEffects)
                {
                    EditorGUILayout.ObjectField(effect, typeof(RenderEffect), true, new GUILayoutOption[0]);
                }
                GUILayoutEx.EndContents();
                if (GUILayout.Button("Select", new GUILayoutOption[0]))
                {
                    List<GameObject> list = new List<GameObject>();
                    foreach (var effect in this.allRendererEffects)
                    {
                        list.Add(effect.gameObject);
                    }
                    if (list.Count > 0)
                    {
                        Selection.objects = list.ToArray();
                    }
                }
            }
        }

    }
}

